package com.xiaomi.back.controller;

import com.xiaomi.back.service.business.BusinessService;
import com.xiaomi.pojo.Business;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@Controller
public class BackLoginController {

    @Autowired
    private BusinessService businessService;


    //后台登陆
    @PostMapping("/login_back")
    public String getLogin(@RequestParam("userName") String bphone, @RequestParam("passWord") String password, String code, HttpSession session) {
        String code_key = (String) session.getAttribute(KAPTCHA_SESSION_KEY);
        session.removeAttribute(KAPTCHA_SESSION_KEY);
        if (code.equals(code_key)) {
            Business business = businessService.Login(bphone, password);
            if (business == null) {
                session.setAttribute("err", "用户名或密码错误！");
                return "forward:/back_login.jsp";
            } else {
                session.setAttribute("business", business);
                return "back/frame";
            }
        } else {
            session.setAttribute("err", "验证码错误！");
            return "forward:/back_login.jsp";
        }
    }

    //跳转个人信息页面
    @RequestMapping("/Info_back")
    public String Info_back(){
        return "back/Personal/personal_data";
    }

    //跳转 修改密码页面
    @RequestMapping("/ToPassword")
    public String ToPassword(){
        return "back/Personal/UpdatePass";
    }
    //后台修改密码
    @PostMapping("/UpPassword")
    public String UpPassword(String oldpassword, String password, String password2, Model model, HttpSession session){

        //比较俩个密码 是否相同
        if(password.equals(password2)){
            Business LoginBusiness=(Business) session.getAttribute("business");
            String Mypassword=LoginBusiness.getPassword();
            String phone=LoginBusiness.getBphone();
            if(Mypassword.equals(DigestUtils.md5DigestAsHex(oldpassword.getBytes()))){
                //修改密码
                int count=businessService.UpPass(LoginBusiness.getBuid(),password);
                session.removeAttribute("business");
                Business business=businessService.Login(phone,password);
                session.setAttribute("business",business);
                model.addAttribute("upstate","修改成功！");
            }else{
                model.addAttribute("upstate","原密码不正确!");
            }
        }else{
            model.addAttribute("upstate","俩次输入得密码不正确！");
        }
        return "back/Personal/UpdatePass";
    }

    //退出登陆
    @RequestMapping("/Back_remove")
    public String ToLogin(HttpSession session){
        session.invalidate();
        return "forward:/back_login.jsp";
    }
}
